package com.scr1pt.fileshare2.model

import com.alibaba.fastjson.annotation.JSONField
import com.fasterxml.jackson.annotation.JsonProperty
import com.scr1pt.extension.format
import com.scr1pt.extension.round
import java.util.*

/**
 * 说明: 文件表格项数据
 *
 * 创建者: tum0r
 *
 * 时间: 2022/2/3 21:57
 */
class FileTableItem(val path: String, val downloadLink: String, val fileName: String, val isDir: Boolean, val qrCode: String, @JSONField(serialize = false, deserialize = false) val fileByteSize: Long, @JSONField(serialize = false, deserialize = false) val lastModifyTime: Date) : Comparable<FileTableItem> {
    @get:JsonProperty("ID")
    var ID: Int = 0
    var fileSize: String? = null
    var time: String? = null


    fun calc() {
        fileSize = when (fileByteSize) {
            in 0 until 1024 -> "$fileByteSize B"
            in 1024 until 1048576 -> "${round(fileByteSize / 1024.0, 2)} KB"
            in 1048576 until 1073741824 -> "${round(fileByteSize / 1048576.0, 2)} MB"
            else -> "${round(fileByteSize / 1073741824.0, 2)} GB"
        }
        time = lastModifyTime.format()
    }

    override fun compareTo(other: FileTableItem): Int = other.lastModifyTime.time.compareTo(this.lastModifyTime.time)
}